<script setup>
import { useUserStore } from '@/stores/user';
// eslint-disable-next-line no-unused-vars
import { Toast } from 'vant';
import api4j from '@/api/example';
import { EiInfo } from '@eplat/ei';
import JWeixin from 'weixin-js-sdk';
import { queryUrlParam } from '@/utils/tools';
// eslint-disable-next-line no-unused-vars
const router = useRouter();
// eslint-disable-next-line no-unused-vars
const user = useUserStore();
const account = ref('');
const password = ref('');
const finish = ref(false);
const loading = ref(false);

// 授权
JWeixin.config({
  debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
  appId: api4j.APPID, // 必填，公众号的唯一标识
  timestamp: '', // 必填，生成签名的时间戳
  nonceStr: '', // 必填，生成签名的随机串
  signature: '', // 必填，签名
  jsApiList: [] // 必填，需要使用的JS接口列表
});
JWeixin.ready(async () => {
  // 获取用户code
  const code = queryUrlParam('code');
  // 没有授权code
  if (code == null) {
    // Toast('授权失败');
    // 跳转到授权页面
    window.location.href =
      'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
      api4j.APPID +
      '&redirect_uri=https%3A%2F%2F' +
      api4j.SERVER_DOMAIN +
      '%2F%23%2FWX%2FGZ%2FWXGZ01&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&forcePopup=true';
  } else {
    // 本地没有token新授权
    if (user.getToken == '' || user.getToken == undefined) {
      // Toast('正在授权');
      const postEiInfo = new EiInfo();
      postEiInfo.set('code', code);
      // 通过code获取用户信息
      await api4j.wxLogin(postEiInfo).then((res) => {
        // 如果授权无效 重新授权
        if (res.status !== 1) {
          // Toast(res.msg)
          window.location.href =
            'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
            api4j.APPID +
            '&redirect_uri=https%3A%2F%2F' +
            api4j.SERVER_DOMAIN +
            '%2F%23%2FWX%2FGZ%2FWXGZ01&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&forcePopup=true';
          return;
        }
        // Toast(res.msg);
        // token info存放本地
        user.setUserInfo(
          res.extAttr.token,
          res.extAttr.userInfo.attrs.headimgurl,
          res.extAttr.userInfo.attrs.nickname
        );
        user.setUserNameTel(res.extAttr.username, res.extAttr.phone);

        onLoad();
      });
    }
  }
});
// eslint-disable-next-line no-unused-vars
const flag = ref(false);
const onLoad = async () => {
  if (!flag.value) {
    loading.value = true;
    applyList();
    loading.value = false;
  }
};
const applyList = async () => {
  const postEiInfo = new EiInfo();
  // eslint-disable-next-line no-unused-vars
  const token = user.getToken;
  if (!token.length == 0) {
    console.log('进入程序');
    postEiInfo.set('token', token);
    console.log('token', token);
    await api4j
      .RCRM0103_isFlogin(postEiInfo)
      .then((res) => {
        if (res.status == 1) {
          console.log('res', res);
          console.log('res', res.extAttr.account);
          user.setGZCode(res.extAttr.account);
          flag.value = true;
          router.push({ path: '/WX/GZ/WXGZ02', state: { account: res.extAttr.account } });
        }
      })
      .catch((error) => {
        if (error.response && error.response.status === 429) {
          // eslint-disable-next-line no-undef
          Toast('请勿频繁操作');
        }
      });
  }
};

/**
 * 登录
 * @returns {Promise<void>}
 */
const onSubmit = async () => {
  const postEiInfo = new EiInfo();
  postEiInfo.set('accounts', account.value);
  postEiInfo.set('password', password.value);
  await api4j.RCRM0103_login(postEiInfo).then((res) => {
    // eslint-disable-next-line eqeqeq
    if (res.msg == '登录成功!') {
      Toast(res.msg);
      // 缓存
      user.clearZCCode();
      user.setGZCode(account.value);
      router.push({ path: '/WX/GZ/WXGZ02', state: { account: account.value } });
      return;
    }
    // 系统登录成功
    if (res.msg == '系统登录成功!') {
      Toast(res.msg);
      // 缓存
      user.clearZCCode();
      user.setGZCode(account.value);
      router.push({ path: '/WX/GZ/WXGZ03', state: { account: account.value } });
      return;
    }
    Toast(res.msg);
    router.push({ path: '/WX/GZ/WXGZ01' });
  });
};
</script>
<!--../../../../src/assets/gzbj1.png-->
<template>
  <van-list
    v-model:loading="loading"
    class="working-list"
    :finished="finish"
    finished-text=""
    offset="1"
    @load="onLoad"
  >
    <div style="width: 100%; height: 100vh; overflow: hidden">
      <div
        style="
          width: 100%;
          height: 100%;
          background-image: url('./assets/gzbj1.png');
          background-size: cover;
          background-position: center;
          display: flex;
          justify-content: center;
          align-items: center;
        "
      >
        <div
          style="
            background: white;
            width: 80%;
            height: 370px;
            box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.5);
            border: 1px solid white;
            border-radius: 10px;
            margin-top: -80px;
          "
        >
          <div style="text-align: center; margin-top: 40px">
            <span style="font-size: 18px; font-weight: bold; color: #508efe"
              >登录江厦星光汇一站式服务平台</span
            >
          </div>

          <div style="background: white">
            <van-form class="form-card" @submit="onSubmit">
              <van-cell-group class="form-cell-group" inset>
                <!--        <van-field v-model="user.$state.username" label="送 检 人" readonly />-->
                <van-field
                  v-model="account"
                  label="账号："
                  placeholder="请输入账号"
                  style="
                    border: 1px solid #cfcfcf;
                    border-radius: 10px;
                    margin-top: 30px;
                    margin-bottom: 30px;
                  "
                  :rules="[{ required: true, message: '请输入账号' }]"
                />
                <van-field
                  v-model="password"
                  label="密码："
                  type="password"
                  placeholder="请输入密码"
                  style="border: 1px solid #cfcfcf; border-radius: 10px; margin-bottom: 30px"
                  :rules="[{ required: true, message: '请输入密码' }]"
                />
              </van-cell-group>
              <div style="text-align: center">
                <van-button
                  native-type="submit"
                  round
                  style="background: #508efe; color: white; width: 80%"
                  >登录</van-button
                >
              </div>
            </van-form>
          </div>
        </div>
      </div>
    </div>
  </van-list>
</template>

<style lang="stylus" scoped>
.page-main

  .working-list
    .item1
      padding-top 0px
      padding-bottom 0px
    .title1
      color #003586
      font-weight bolder
      font-size 13px
    .titles
      color #161718
      font-weight bolder
      font-size 13px
    .titless
      color #161718
      font-weight bolder
      font-size 17px
    .title2
      color #808080
      font-size 13px
    padding 4px
    margin-bottom 48px
    .van-tag
      margin-left 5px
      font-size 12px
      padding 5px
    .btn1
      background-color #038008
      border-radius 5px
      margin-left 5px
    .btn0
      background-color #ff8d1a
      border-radius 5px
      margin-left 5px
    .class1
      background-color #99bcf1
      border-radius 5px
    .class2
      background-color #f19999
      border-radius 5px
    .class3
      background-color #99a6f1
      border-radius 5px
    .card-item
      margin 8px
      background-color white

      .item-value
        display flex
        justify-content space-between
        overflow hidden
        text-overflow ellipsis
        word-break keep-all
        white-space nowrap
        background-color transparent
        .group1
          display flex
          flex-direction row
          justify-content space-between

  .bottom-buttons
    position fixed
    bottom 12px
    left auto
    width 100%
    max-width var(--max-width)
</style>
